/*-----------------------------------------------------------------------------------------------------------
Description :分配式可控硅丢波控制 适用于纯阻性负责控制 
-------------------------------------------------------------------------------------------------------------*/
#include "PLT_HEADS.h"
#include "TK_ScrCtrl.h"
/*---------------------------------------------------------------------------------------------------------*/
#if (Scrctrlmode == 0)
unsigned char U8_ScrOpenArry[N_CYCLE] = {0};
#else
unsigned int  Pre = (N_CYCLE/(n_FREQUENCYY/2)) - 20;
unsigned char U8_ScrDelayOpenTime = 0;
#endif
ScrCtrlContext ScrCtrl;
/*---------------------------------------------------------------------------------------------------------*/
/*===========================================================================================================
Function    : SCR_Control
Input       : percent - 可控硅输出百分比 (0-100)
Output      : 无
Description : 根据输出百分比设置可控硅导通逻辑（丢波或斩波控制）
===========================================================================================================*/
void SCR_Control(unsigned char percent)
{
    // 输入边界检查
    double dutyRatio,idleRatio;
    if (percent > 100) 
    {
        percent = 100;
    }
    if (ScrCtrl.LastScrPercent == percent) 
    {
        return;
    }
    ScrCtrl.LastScrPercent = percent;
    #if (Scrctrlmode == 1) // 斩波控制
    {
        double delayTime;
        dutyRatio = (double)percent / 100.0;
        idleRatio = 1.0 - dutyRatio;
        delayTime = idleRatio * Pre;
        U8_ScrDelayOpenTime = (unsigned char)delayTime;
    }
    #else // 丢波控制
    {
        double idx;
        // 清空数组
        for (idx = 0; idx < N_CYCLE; idx++) 
        {
            U8_ScrOpenArry[(unsigned char)idx] = 0;
        }
        dutyRatio = (double)percent / 100.0;
        idleRatio = 1.0 / dutyRatio;
        for (idx = 0; idx < N_CYCLE; idx+=idleRatio) 
        {
            U8_ScrOpenArry[(unsigned char)idx] = 1;
        }
    }
    #endif
}